<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>回收站地图</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://lib.baomitu.com/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://lib.baomitu.com/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #f8f9fa;
        }
        .map-container {
            width: 100%;
            height: 100vh;
            position: relative;
        }
        #baiduMap {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
        }
        .station-list {
            position: absolute;
            top: 20px;
            left: 20px;
            width: 300px;
            max-height: calc(100vh - 40px);
            background: white;
            border-radius: 15px;
            padding: 1rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            overflow-y: auto;
            z-index: 1000;
        }
        .station-item {
            padding: 1rem;
            border-bottom: 1px solid #eee;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .station-item:hover {
            background-color: #f8f9fa;
        }
        .station-item:last-child {
            border-bottom: none;
        }
        .station-name {
            font-weight: 500;
            margin-bottom: 0.5rem;
        }
        .station-info {
            color: #6c757d;
            font-size: 0.9rem;
        }
        .station-status {
            padding: 0.25rem 0.5rem;
            border-radius: 10px;
            font-size: 0.8rem;
        }
        .status-open {
            background-color: #d1e7dd;
            color: #198754;
        }
        .status-closed {
            background-color: #f8d7da;
            color: #dc3545;
        }
    </style>
</head>
<body>
    <div class="map-container">
        <!-- 回收站列表 -->
        <div class="station-list">
            <div class="search-box mb-3">
                <input type="text" class="form-control" placeholder="搜索回收站..." 
                       onkeyup="searchStations(this.value)">
            </div>
            <div id="stationList">
                <div class="station-item" th:each="station : ${stations}" 
                     th:onclick="'showStationInfo(' + ${station.id} + ')'">
                    <div class="d-flex justify-content-between align-items-start">
                        <div class="station-name" th:text="${station.name}">回收站名称</div>
                        <span class="station-status" 
                              th:classappend="${station.status == 1 ? 'status-open' : 'status-closed'}"
                              th:text="${station.status == 1 ? '营业中' : '已关闭'}">营业中</span>
                    </div>
                    <div class="station-info">
                        <div class="mb-1">
                            <i class="fas fa-map-marker-alt me-2"></i>
                            <span th:text="${station.address}">回收站地址</span>
                        </div>
                        <div>
                            <i class="fas fa-clock me-2"></i>
                            <span th:text="${station.businessHours}">营业时间：09:00-18:00</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 百度地图 -->
        <div id="baiduMap"></div>
    </div>

    <script src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://lib.baomitu.com/twitter-bootstrap/5.2.3/js/bootstrap.bundle.min.js"></script>
    <script type="text/javascript">
        window.BMAP_AUTHENTIC_KEY = /*[[${@baiduMapConfig.apiKey}]]*/ '';
    </script>
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&type=webgl&ak=mNFuffn83uuKvzWesj3uqfVho6CVP9LI"></script>
    <script th:src="@{/js/map-component.js}"></script>
    <script th:inline="javascript">
        let mapComponent;
        const stations = /*[[${stations}]]*/ [];
        
        document.addEventListener('DOMContentLoaded', async () => {
            try {
                mapComponent = new BaiduMapComponent('baiduMap', {
                    enableGeolocation: true,
                    onMapInitialized: function(map) {
                        // 添加所有回收站标记
                        stations.forEach(station => {
                            const content = `
                                <div class="p-3">
                                    <h5>${station.name}</h5>
                                    <p><i class="fas fa-map-marker-alt me-2"></i>${station.address}</p>
                                    <p><i class="fas fa-clock me-2"></i>${station.businessHours}</p>
                                    <p><i class="fas fa-phone me-2"></i>${station.contactPhone}</p>
                                    <p><i class="fas fa-recycle me-2"></i>${station.acceptableItems || '所有可回收物品'}</p>
                                    <button class="btn btn-primary btn-sm" onclick="startNavigation(${station.latitude}, ${station.longitude})">
                                        导航到这里
                                    </button>
                                </div>
                            `;
                            
                            mapComponent.addMarker(`station-${station.id}`, 
                                new BMapGL.Point(station.longitude, station.latitude),
                                {
                                    infoWindow: {
                                        content: content,
                                        title: station.name
                                    }
                                }
                            );
                        });
                    }
                });
                
                await mapComponent.init();
            } catch (error) {
                console.error('地图初始化失败:', error);
            }
        });

        // 显示回收站信息
        function showStationInfo(stationId) {
            const station = stations.find(s => s.id === stationId);
            if (station) {
                mapComponent.setView([station.longitude, station.latitude], 15);
                const marker = mapComponent.markers.get(`station-${stationId}`);
                const infoWindow = mapComponent.infoWindows.get(`station-${stationId}`);
                if (marker && infoWindow) {
                    mapComponent.map.openInfoWindow(infoWindow, marker.getPosition());
                }
            }
        }

        // 搜索回收站
        function searchStations(keyword) {
            const items = document.querySelectorAll('.station-item');
            items.forEach(item => {
                const name = item.querySelector('.station-name').textContent;
                const address = item.querySelector('.station-info span').textContent;
                if (name.toLowerCase().includes(keyword.toLowerCase()) || 
                    address.toLowerCase().includes(keyword.toLowerCase())) {
                    item.style.display = '';
                } else {
                    item.style.display = 'none';
                }
            });
        }

        // 开始导航
        function startNavigation(targetLat, targetLng) {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(
                    (position) => {
                        const start = [position.coords.longitude, position.coords.latitude];
                        const end = [targetLng, targetLat];
                        mapComponent.addRoute(start, end);
                    },
                    (error) => {
                        console.error('获取位置失败:', error);
                        alert('无法获取您的位置，请检查位置权限设置');
                    }
                );
            } else {
                alert('您的浏览器不支持地理定位');
            }
        }
    </script>
</body>
</html> 